package com.serialcommunication.gui.frame;

import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JSeparator;

import com.serialcommunication.constants.ActionConstants;
import com.serialcommunication.enums.Engine;
import com.serialcommunication.enums.Mode;
import com.serialcommunication.gui.Application;
import com.serialcommunication.gui.component.BaseComponent;
import com.serialcommunication.interfaces.IBaseComponent;
import com.serialcommunication.interfaces.INotifyComponent;
import com.serialcommunication.interfaces.ITransmitComponent;
import com.serialcommunication.model.ComponentRow;

import static com.serialcommunication.constants.GuiConfiguration.*;

public class MainFrame extends BaseFrame {

	private List<ComponentRow> componentRows_Engine1 = new ArrayList<ComponentRow>();
	private List<ComponentRow> componentRows_Engine2 = new ArrayList<ComponentRow>();
	private List<ComponentRow> componentRows_Engine3 = new ArrayList<ComponentRow>();

	public List<IBaseComponent> rampModeComponents_Engine1 = new ArrayList<IBaseComponent>();
	public List<IBaseComponent> rampModeComponents_Engine2 = new ArrayList<IBaseComponent>();
	public List<IBaseComponent> rampModeComponents_Engine3 = new ArrayList<IBaseComponent>();

	public List<IBaseComponent> velocityModeComponents_Engine1 = new ArrayList<IBaseComponent>();
	public List<IBaseComponent> velocityModeComponents_Engine2 = new ArrayList<IBaseComponent>();
	public List<IBaseComponent> velocityModeComponents_Engine3 = new ArrayList<IBaseComponent>();

	public MainFrame(Application pWindow) {
		super(pWindow);
		initLists();
	}

	private void createComponentRow(String pEngine) {
		if (pEngine.equals("1")) {
			ComponentRow row = new ComponentRow(PANEL_X_ENGINE_1, getSumOfHeight("1"));
			componentRows_Engine1.add(row);
		} else {
			// engine 2 and 3
		}
	}

	/**
	 * adding Components with its panel to the frame and positions the panel
	 * 
	 * @param rowNumber
	 * @param comp
	 */
	public void addComponent(String pEngine, int rowNumber, IBaseComponent comp) {
		if (pEngine.equals("1")) {
			if (componentRows_Engine1.size() < rowNumber) {
				createComponentRow("1");
			}
			Point p = componentRows_Engine1.get(--rowNumber).insert(comp);
			comp.getPanel().setLocation(p);
			add(comp.getPanel());
		} else {
			// engine 2 and 3
		}
	}

	public void addComponent(String pEngine, int rowNumber, JComponent comp) {
		if (pEngine.equals("1")) {
			if (componentRows_Engine1.size() < rowNumber) {
				createComponentRow("1");
			}
			Point p = componentRows_Engine1.get(--rowNumber).insert(comp);
			comp.setLocation(p);
			add(comp);
		} else {
			// engine 2 and 3
		}
	}

	public void setModeForEngine(Engine pEngine, Mode pMode) {
		if (pEngine.equals(Engine.ENGINE_1)) {
			for (ComponentRow row : componentRows_Engine1) {
				for (IBaseComponent comp : row.getList()) {
					if (pMode.equals(Mode.RAMP)) {
						for (IBaseComponent c : velocityModeComponents_Engine1) {
							((ITransmitComponent) c).disableComponent();
						}
						for (IBaseComponent c : rampModeComponents_Engine1) {
							((ITransmitComponent) c).enableComponent();
						}
						if (comp.getName().equals(ActionConstants.RAMP_SOFT_MODE.getName())) {
							((ITransmitComponent) comp).setInActive();
						}
						if (comp.getName().equals(ActionConstants.VELOCITY_MODE.getName())) {
							((ITransmitComponent) comp).setInActive();
						}
					} else if (pMode.equals(Mode.RAMP_SOFT)) {
						for (IBaseComponent c : velocityModeComponents_Engine1) {
							((ITransmitComponent) c).disableComponent();
						}
						for (IBaseComponent c : rampModeComponents_Engine1) {
							((ITransmitComponent) c).enableComponent();
						}
						if (comp.getName().equals(ActionConstants.RAMP_MODE.getName())) {
							((ITransmitComponent) comp).setInActive();
						}
						if (comp.getName().equals(ActionConstants.VELOCITY_MODE.getName())) {
							((ITransmitComponent) comp).setInActive();
						}
					} else if (pMode.equals(Mode.VELOCITY)) {
						for (IBaseComponent c : rampModeComponents_Engine1) {
							((ITransmitComponent) c).disableComponent();
						}
						for (IBaseComponent c : velocityModeComponents_Engine1) {
							((ITransmitComponent) c).enableComponent();
						}
						if (comp.getName().equals(ActionConstants.RAMP_SOFT_MODE.getName())) {
							((ITransmitComponent) comp).setInActive();
						}
						if (comp.getName().equals(ActionConstants.RAMP_MODE.getName())) {
							((ITransmitComponent) comp).setInActive();
						}
					}
				}
			}
		} else {
			// other engines
		}
	}

	public void linkOk() {

		for (ComponentRow row : componentRows_Engine1) {
			for (IBaseComponent comp : row.getList()) {
				if (comp instanceof ITransmitComponent) {
					((ITransmitComponent) comp).enableComponent();
				}
				if (comp instanceof INotifyComponent) {
					if (comp.getTitle().equals("LINK_OK")) {
						((INotifyComponent) comp).setColorPositive();
					}
				}
			}
		}
	}

	public void linkNotOk() {

		for (ComponentRow row : componentRows_Engine1) {
			for (IBaseComponent comp : row.getList()) {
				if (comp instanceof ITransmitComponent) {
					((ITransmitComponent) comp).disableComponent();
				}
				if (comp instanceof INotifyComponent) {
					if (comp.getTitle().equals("LINK_OK")) {
						((INotifyComponent) comp).setColorNegative();
					}
				}
			}
		}
	}

	private int getSumOfHeight(String pEngine) {
		int retValue = 0;
		int i = 0;
		if (pEngine.equals("1")) {
			for (ComponentRow row : componentRows_Engine1) {
				retValue = retValue + row.getHeigth();
				i++;
			}
		} else {
			// engine 2 and 3
		}
		retValue = retValue + PANEL_FIRST_Y + (PANEL_PANEL_PADDING_Y * i);
		return retValue;
	}

	private void initLists() {
		componentRows_Engine1.add(new ComponentRow(PANEL_X_ENGINE_1, PANEL_FIRST_Y));
	}
}
